home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / hexify.arc / dehex.c next >
C/C++ Source or Header  |  1989-03-31  |  1KB  |  72 lines

  1. /*
  2.  *  Hex-to-binary utility -- conquer BITNET mailers!!
  3.  *
  4.  *  Restore binary file from a text file with hexadecimal digits
  5.  *
  6.  *  Michal Jaegermann, 19 March 1989
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #define  BSIZE 128
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char **argv;
  16. {
  17.     int ch, count = 0;
  18.     FILE *fin, *fout;
  19.     char inbuf[BSIZE+2];
  20.     char outname[14];
  21.     
  22.     if (argc != 2) {
  23.         fprintf(stderr, "usage: %s <infile>\n", argv[0]);
  24.         exit(1);
  25.     }
  26.     
  27.     if (NULL == (fin = fopen(argv[1],"r"))){
  28.         fprintf(stderr, "cannot find input file %s\n", argv[1]);
  29.         exit(1);
  30.     }
  31.     /* search for BEGIN line */
  32.     for(;;) {
  33.         if (NULL == fgets(inbuf, BSIZE, fin)){
  34.             fprintf(stderr, "No BEGIN line\n");
  35.             fclose(fin);
  36.             exit(1);
  37.         }
  38.         sscanf(inbuf, " %12s", outname);
  39.         if (0 == strcmp("BEGIN", outname)) break;
  40.         if ('\n' != inbuf[strlen(inbuf) - 1]) {
  41.             while ('\n' != (ch = getc(fin))) {
  42.                 ; /* eat a remainder of a line */
  43.             }
  44.         }
  45.     }
  46.     
  47.     /* get an output file name */
  48.     sscanf(inbuf, " %*5s %12s", outname);    
  49.     
  50.     if (NULL == (fout = fopen(outname,"wb"))){
  51.         fprintf(stderr, "cannot open output file %s", outname);
  52.         fclose(fin);
  53.         exit(1);
  54.     }
  55.     
  56.     /* do the actual job */    
  57.     while (1 == fscanf(fin, " %02x", &ch)) {
  58.         putc((char) ch, fout);
  59.     }
  60.     fclose(fout);
  61.  
  62.     /* just a check for completeness */
  63.     fscanf(fin, " %3s", inbuf);
  64.     if (0 != strcmp("ND", inbuf)) { /* a little bit of cheating */
  65.         fprintf(stderr, "%s -- END line not found in %s\n",
  66.                 inbuf, argv[1]);
  67.     }
  68.     fclose(fin);
  69.     exit(0);
  70. }
  71.  
  72.